home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / 3D_GRAPH / G3D_SETU.C < prev    next >
Text File  |  1989-08-28  |  7KB  |  406 lines

  1. /*
  2.     Copyright '89    Christopher Moll
  3.     all rights reserved
  4. */
  5.  
  6.  
  7. #    include <ColorToolbox.h>
  8.  
  9. #include    "graph3D.h"
  10. #ifndef    _LSC3_
  11. #    include <WindowMgr.h>
  12. #    include <EventMgr.h>
  13. #    include <MenuMgr.h>
  14. #    include <FileMgr.h>
  15. #    include <TextEdit.h>
  16. #    include <ScrapMgr.h>
  17. #endif
  18.  
  19.  
  20.  
  21. /* windows */
  22. extern    GrafPtr            windManPort;
  23.  
  24. extern    WindowPtr        graphWind;
  25. WindowRecord    fwindRecord;
  26. WindowRecord    gwindRecord;
  27.  
  28.  
  29. extern    Point        *graphPoints;    /* same elements as funcResults */
  30.  
  31. extern    Real    startX, startY;
  32. extern    Real    endX, endY;
  33. extern    Real    deltaX, deltaY;
  34. extern    int        numX, numY;
  35.  
  36. extern    Boolean        vectrsCurrent;
  37.  
  38. extern    Vector        maxVect, minVect;
  39. extern    Real        rotMatrx[3][3];
  40. extern    Boolean        functCurrent;
  41.  
  42. extern    Real        *funcResults;
  43. extern    Vector        *scalVectResults;
  44.  
  45.  
  46.  
  47. /* menus */
  48. extern    MenuHandle        appleMenu, fileMenu, editMenu, plotMenu,
  49.                         gTypeMenu, windwMenu;
  50.  
  51. extern    int            scrapCompare;    /* status of the deskscrap (clipboard) */
  52. extern    Boolean        scrapDirty;        /* deskscrap has been changed */
  53.  
  54. extern    Boolean            inMultiFndr;
  55. extern    Boolean            colorQD, scrnColor;
  56.  
  57. extern    PatternList    *greyList;
  58. extern    int        numGrays;
  59.  
  60. extern    long    MoveToAddr, LineToAddr, FixMulAddr;
  61.  
  62.  
  63. DoSetup()
  64. {
  65.     Init();
  66.     OpenSerPort();
  67.     AddrsSet();
  68.  
  69.     scrapCompare = ScrapInfo.scrapCount + 1;
  70.     scrapDirty = FALSE;
  71.     ReadDeskScrap();
  72.  
  73.     InitColor();
  74.     MakeWindows();
  75.     MakeMenus();
  76.     InitPts();
  77.     CheckMultiFndr();
  78. }
  79.  
  80. static
  81. InitColor()
  82. {
  83.     PattListHand    greyHndl;
  84.     SysEnvRec        theWorld;
  85.     GDHandle        grefDev;
  86.     int                depth;
  87.     Boolean        ScrnHasColor();
  88.  
  89.     greyHndl = (PattListHand)GetResource('PAT#', 256);
  90.     if (greyHndl)
  91.     {
  92.         MoveHHi(greyHndl);
  93.         HLock(greyHndl);
  94.         greyList = *greyHndl;
  95.     }
  96.  
  97.     SysEnvirons(1, &theWorld);
  98. #ifdef    _MC68881_
  99.     if (theWorld.processor < env68020)
  100.     {
  101.         Alert(ALERT_020, NIL);
  102.         ExitToShell();
  103.     }
  104.     else if (NOT(theWorld.hasFPU))
  105.     {
  106.         Alert(ALERT_881, NIL);
  107.         ExitToShell();
  108.     }
  109. #endif
  110.     colorQD = theWorld.hasColorQD;
  111.     scrnColor = ScrnHasColor();
  112. }
  113.  
  114. Boolean
  115. ScrnHasColor()
  116. {
  117.     int                depth;
  118.     GDHandle        grefDev;
  119.  
  120.     if (colorQD)
  121.     {
  122.         grefDev = GetGDevice();
  123.         depth = (**((**grefDev).gdPMap)).pixelSize;
  124.         return(depth > 1);
  125.     }
  126.     else
  127.         return(FALSE);
  128. }
  129.  
  130. static
  131. CheckMultiFndr()
  132. {
  133.     long    WaitAdrs, unimpAdrs;
  134.  
  135.     WaitAdrs = GetTrapAddress(0x60);
  136.     unimpAdrs = GetTrapAddress(0x9f);
  137.     if (WaitAdrs EQ unimpAdrs)
  138.         inMultiFndr = FALSE;
  139.     else
  140.         inMultiFndr = TRUE;
  141. }
  142.  
  143.  
  144. Init()
  145. {
  146.     InitGraf(&thePort);
  147.     InitFonts();
  148.     InitWindows();
  149.     InitMenus();
  150.     TEInit();
  151.     InitDialogs(0L);
  152.     InitCursor();
  153.  
  154.     FlushEvents(everyEvent, 0);
  155.  
  156.     GetWMgrPort(&windManPort);    /* get whole screen port that window mgr
  157.                    uses */
  158.     SetPort(windManPort);    /* and start off with it */
  159.  
  160.     SetEventMask(everyEvent);
  161. }
  162.  
  163. MakeWindows()
  164. {
  165.     Rect editRect;
  166.  
  167.     ShowSpecs();
  168.     if (colorQD)
  169.         graphWind = GetNewCWindow(WIND_GRAPH, &gwindRecord, -1L);
  170.     else
  171.         graphWind = GetNewWindow(WIND_GRAPH, &gwindRecord, -1L);
  172.     ShowWindow(graphWind);
  173.     SetPort(graphWind);
  174.     SetGraphRect();
  175.     ShowLimits();
  176.     ShowFunctDial();
  177. }
  178.  
  179. MakeMenus()
  180. {
  181.     appleMenu = GetMenu(MENU_APPLE);
  182.     AddResMenu(appleMenu, 'DRVR');
  183.     InsertMenu(appleMenu, 0);
  184.  
  185.     fileMenu = GetMenu(MENU_FILE);
  186.     InsertMenu(fileMenu, 0);
  187.  
  188.     editMenu = GetMenu(MENU_EDIT);
  189.     InsertMenu(editMenu, 0);
  190.  
  191.     plotMenu = GetMenu(MENU_PLOT);
  192.     InsertMenu(plotMenu, 0);
  193.  
  194.     gTypeMenu = GetMenu(MENU_GTYP);
  195.     InsertMenu(gTypeMenu, 0);
  196.     CheckItem(gTypeMenu, 1, TRUE);
  197.  
  198.     windwMenu = GetMenu(MENU_WIND);
  199.     InsertMenu(windwMenu, 0);
  200.     
  201.     CheckItem(plotMenu, PLOT_WIREFRAM, TRUE);
  202.  
  203.     DrawMenuBar();
  204. }
  205.  
  206. InitPts()
  207. {
  208.     register    long    i, numElems;
  209.     register    Real    *resltsPtr;
  210.  
  211.     MkRotMat(0.0, -.3, -.3, rotMatrx);
  212.  
  213.     GetSpecDCont();
  214.     GetLimtDCont();
  215.  
  216.     numX = numY = 20;
  217.  
  218.     deltaX = (endX - startX) / numX;
  219.     deltaY = (endY - startY) / numY;
  220.  
  221.     funcResults = (Real *)NewPtr((long)sizeof(Real) * (long)numX * numY);
  222.     if (MemErr)
  223.         goto Fail;
  224.     scalVectResults = (Vector *)NewPtr((long)sizeof(Vector) * (long)numX * numY);
  225.     if (MemErr)
  226.         goto Fail;
  227.     graphPoints = (Point *)NewPtr((long)sizeof(Point) * (long)numX * numY);
  228.     if (MemErr)
  229.         goto Fail;
  230.  
  231.     resltsPtr = funcResults;
  232.     numElems = (long)numX * numY;
  233.     for (i = 0; i < numElems; i++)
  234.         *resltsPtr++ = 0.0;
  235.  
  236.     maxVect.x = 0.0;
  237.     maxVect.y = 0.0;
  238.     maxVect.z = 0.0;
  239.     minVect.x = 0.0;
  240.     minVect.y = 0.0;
  241.     minVect.z = 0.0;
  242.  
  243.     vectrsCurrent = FALSE;
  244.     functCurrent = TRUE;
  245.     return;
  246.  
  247. Fail:
  248.     GenralAlert("\PToo little memory to run; returning to Finder");
  249.     ExitToShell();
  250. }
  251.  
  252.  
  253.  
  254. static    long    rtrnAdrs;
  255. static    unsigned    char    buffer[256];
  256.  
  257. tprintf()
  258. {
  259.     asm    {
  260.         move.l    (sp)+,rtrnAdrs
  261.         }
  262.     sprintf(buffer);
  263.     SendPort(buffer, strlen(buffer));
  264.     asm    {
  265.         move.l    rtrnAdrs,a0
  266.         jmp        (a0)
  267.         }
  268. }
  269.  
  270. scprintf()
  271. {
  272.     asm    {
  273.         move.l    (sp)+,rtrnAdrs
  274.         }
  275.     sprintf(buffer);
  276.     My_printf(buffer);
  277.     asm    {
  278.         move.l    rtrnAdrs,a0
  279.         jmp        (a0)
  280.         }
  281. }
  282.  
  283. /*flprintf()
  284. {
  285.     asm    {
  286.         move.l    (sp)+,rtrnAdrs
  287.         }
  288.     sprintf(buffer);
  289.     AppendTempFile(buffer);
  290.     asm    {
  291.         move.l    rtrnAdrs,a0
  292.         jmp        (a0)
  293.         }
  294. }*/
  295.  
  296. static
  297. _My_printf(shwStr)
  298. char    *shwStr;
  299. {
  300.     char    buffer[256];
  301. static    Rect    dispRect = {67, 54, 87, 166};
  302.  
  303.     ClipRect(&dispRect);
  304.     EraseRect(&dispRect);
  305.     MoveTo(dispRect.left + 10, dispRect.bottom - 4);
  306.     strcpy(buffer, shwStr);
  307.     ctop(buffer);
  308.     DrawString(buffer);
  309. }
  310.  
  311. static    IOParam        portParam;
  312.  
  313. /*** open printer port ***/
  314. OpenSerPort()
  315. {
  316.     portParam.ioCompletion = 0L;
  317.     portParam.ioNamePtr = (StringPtr)("\p.BOut");
  318.     portParam.ioRefNum = 0;
  319.     portParam.ioPermssn = fsWrPerm;
  320.     portParam.ioMisc = 0L;
  321.  
  322.     PBOpen(&portParam, FALSE);
  323.     if (portParam.ioResult);
  324.         /*if (portParam.ioResult EQ -98)
  325.             GenralAlert("\PAppleTalk connected; won't be able to print");
  326.         
  327.         else
  328.             GenralAlert("\PError opening printer port");*/
  329. }
  330.  
  331. static
  332. OsErr
  333. SendPort(buffer, length)
  334. char    *buffer;
  335. int        length;
  336. {
  337.     portParam.ioBuffer = buffer;
  338.     portParam.ioReqCount = length;
  339.     portParam.ioPosMode = fsAtMark;
  340.  
  341.     PBWrite(&portParam, FALSE);
  342.     buffer[portParam.ioActCount] = '\0';
  343.     return(portParam.ioResult);
  344. }
  345.  
  346.  
  347.  
  348.  
  349. _SetGray(theColor, greyShd)
  350. register    RGBColor    *theColor;
  351. register    int        greyShd;
  352. {
  353.     int        setColor;
  354.  
  355.     setColor = greyShd << 8;
  356.  
  357.     theColor->red = setColor;
  358.     theColor->green = setColor;
  359.     theColor->blue = setColor;
  360. }
  361.  
  362. SetGray(greyShd)
  363. register    int        greyShd;
  364. {
  365.     if (scrnColor)
  366.     {
  367.         if (greyShd EQ 0)
  368.             PmForeColor(1);
  369.         else if (greyShd EQ 255)
  370.             PmForeColor(0);
  371.         else
  372.             PmForeColor(256 - greyShd);
  373.     }
  374.     else
  375.         PenPat(greyList->patList + (63 - greyShd));
  376. }
  377.  
  378.  
  379. __SetGray(greyShd)
  380. register    int        greyShd;
  381. {
  382.     RGBColor    theColor;
  383. register    int        setColor;
  384.  
  385.     if (greyShd < 0)
  386.         setColor = 0;
  387.     else if (greyShd < 12)
  388.         setColor = greyShd * 5462;
  389.     else
  390.         setColor = 0xffff;
  391.  
  392.     theColor.red = setColor;
  393.     theColor.green = setColor;
  394.     theColor.blue = setColor;
  395.     RGBForeColor(&theColor);
  396. }
  397.  
  398. static
  399. AddrsSet()
  400. {
  401.     MoveToAddr = GetTrapAddress(MoveToTrapNum);
  402.     LineToAddr = GetTrapAddress(LineToTrapNum);
  403.     FixMulAddr = GetTrapAddress(FixMulTrapNum);
  404. }
  405.  
  406.